//
//  ExchangeRateConvert.m
//  汇率换算
//
//  Created by king on 15/11/26.
//  Copyright © 2015年 king. All rights reserved.
//

#import "ExchangeRateConvert.h"
#import "ExchangeRateConvertModel.h"

@implementation ExchangeRateConvert

#pragma mark - Method shareExchangeRateConvert
+ (instancetype)shareExchangeRateConvert {
    static ExchangeRateConvert *exchangeRateConvert;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        exchangeRateConvert = [[ExchangeRateConvert alloc] init];
    });
    return exchangeRateConvert;
}
#pragma mark - Method openDB(打开数据库操作)
- (void)openDB {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Preferences/example.sqlite"];
    int result = sqlite3_open([docPath UTF8String], &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"打开数据库成功example.sqlite");
    } else {
        NSLog(@"打开数据库失败example.sqlite");
    }
}
#pragma mark - Method createTableForExchangeRateConvert(创建汇率换算表)
- (void)createTableForExchangeRateConvert {
    NSString *sqlStr = @"create table if not exists exchangeRateConvert (number integer primary key, currencyKey text, currencyValue text)";
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建表createTableForExchangeRateConvert成功");
        // 汇率换算 货币代码表：加入数据库中
        for (ExchangeRateConvertModel *temp in [self currencyCodeFormArr]) {
            [self insertDataToTableExchangeRateConvert:temp];
        }
    } else {
        NSLog(@"创建表createTableForExchangeRateConvert失败");
    }
}
#pragma mark - Method insertDataToTableExchangeRateConvert(添加数据到表ExchangeRateConvert)
- (void)insertDataToTableExchangeRateConvert:(ExchangeRateConvertModel *)exchangeRateConvertModel {
    NSString *sqlStr = [NSString stringWithFormat:@"insert into exchangeRateConvert (currencyKey, currencyValue) values ('%@', '%@')", exchangeRateConvertModel.currencyKey, exchangeRateConvertModel.currencyValue];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"insertDataToTableExchangeRateConvert添加数据成功");
    } else {
        NSLog(@"insertDataToTableExchangeRateConvert添加数据失败");
    }
}
#pragma mark - Method currencyCodeFormArr(返回一个货币代码表数组)
- (NSMutableArray *)currencyCodeFormArr {
    NSArray *array = @[@[@"CNY", @"人民币 (CNY)"], @[@"USD", @"美元 (USD)"], @[@"RUB", @"俄罗斯卢布 (RUB)"], @[@"HKD", @"港元 (HKD)"], @[@"EUR", @"欧元 (EUR)"], @[@"JPY", @"日元 (JPY)"], @[@"KRW", @"韩元 (KRW)"], @[@"KPW", @"朝鲜圆 (KPW)"], @[@"GBP", @"英镑 (GBP)"], @[@"MOP", @"澳门元 (MOP)"], @[@"TWD", @"台币 (TWD)"], @[@"SGD", @"新加坡元 (SGD)"], @[@"THB", @"泰国铢 (THB)"], @[@"AUD", @"澳元 (AUD)"], @[@"AED", @"阿联酋迪拉姆 (AED)"], @[@"ALL", @"阿尔巴尼亚列克 (ALL)"], @[@"FRF", @"法国法郎 (FRF)"]];
    NSMutableArray *currencyCodeFormDataArray = [NSMutableArray array];
    for (NSArray *arrTemp in array) {
        ExchangeRateConvertModel *modelTemp = [[ExchangeRateConvertModel alloc] init];
        modelTemp.currencyKey = [arrTemp firstObject];
        modelTemp.currencyValue = [arrTemp lastObject];
        [currencyCodeFormDataArray addObject:modelTemp];
    }
    return currencyCodeFormDataArray;
}
#pragma mark - Method selectDataFromTableExchangeRateConvert
- (NSMutableArray *)selectDataFromTableExchangeRateConvert {
    NSString *sqlStr = @"select * from exchangeRateConvert";
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *currencyKey = sqlite3_column_text(stmt, 1);
            const unsigned char *currencyValue = sqlite3_column_text(stmt, 2);
            ExchangeRateConvertModel *temp = [[ExchangeRateConvertModel alloc] init];
            temp.currencyKey = [NSString stringWithUTF8String:(const char *)currencyKey];
            temp.currencyValue = [NSString stringWithUTF8String:(const char *)currencyValue];
            [array addObject:temp];
        }
        NSLog(@"selectDataFromTableExchangeRateConvert检索成功");
    } else {
        NSLog(@"selectDataFromTableExchangeRateConvert检索失败");
    }
    sqlite3_finalize(stmt);
    return array;
}
#pragma mark - Method selectDataFromExchangeRateConvertBaseKey
- (NSString *)selectDataFromExchangeRateConvertBaseKey:(NSString *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"select * from exchangeRateConvert where currencyKey = '%@'", model];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSString *str = [NSString string];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *currencyValue = sqlite3_column_text(stmt, 2);
            str = [NSString stringWithUTF8String:(const char *)currencyValue];
        }
        NSLog(@"selectDataFromExchangeRateConvertBaseKey检索成功");
    } else {
        NSLog(@"selectDataFromExchangeRateConvertBaseKey检索失败");
    }
    sqlite3_finalize(stmt);
    return str;
}
@end
